package com.example.project;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.example.project.base.BaseActivity;
import com.example.project.presenter.MainPresenter;
import com.example.project.ui.activity.ChatActivity;
import com.example.project.ui.fragment.DiscoveryFragment;
import com.example.project.view.MainView;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.ui.EaseContactListFragment;
import com.hyphenate.easeui.ui.EaseConversationListFragment;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;

public class MainActivity extends BaseActivity<MainPresenter> implements MainView {


    @BindView(R.id.fl)
    FrameLayout mFl;
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    private ArrayList<Fragment> mFragments;
    private FragmentManager mManager;
    //要隐藏fragmnet的position
    private int mLastFragmentPosition = 0;
    private EaseContactListFragment mContactListFragment;

    //就近原则
    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected MainPresenter initPresenter() {
        //MainPresenter presenter = new MainPresenter();
        //不通过构造传递view层对象,通过方法传递,
        //因为所有p层创建的时候都需要持有view
        //presenter.bindView(this);
        return new MainPresenter();
    }

    @Override
    protected void initData() {
        mPresenter.getContacts();
    }

    @Override
    protected void initView() {
        mManager = getSupportFragmentManager();
        initFragment();
        initTab();

        showConversionFragment();

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switchFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void showConversionFragment() {
        mManager.beginTransaction()
                .add(R.id.fl, mFragments.get(0))
                .commit();
    }

    private void switchFragment(int position) {
        FragmentTransaction transaction = mManager.beginTransaction();
        Fragment showFragment = mFragments.get(position);
        Fragment hideFragment = mFragments.get(mLastFragmentPosition);

        //多次添加会崩
        if (!showFragment.isAdded()) {
            transaction.add(R.id.fl, showFragment);
        }
        transaction.show(showFragment);
        transaction.hide(hideFragment);

        transaction.commit();
        //当前显示的fragment，下次点击tab就是要隐藏的fragmnet
        mLastFragmentPosition = position;
    }

    private void initTab() {

        mTabLayout.addTab(mTabLayout.newTab().setText("会话").setIcon(R.drawable.selector_conversion));
        mTabLayout.addTab(mTabLayout.newTab().setText("联系人").setIcon(R.drawable.selector_contact));
        mTabLayout.addTab(mTabLayout.newTab().setText("发现").setIcon(R.drawable.selector_discovery));
        //mTabLayout.addTab(mTabLayout.newTab().setCustomView());
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        //会话fragment
        EaseConversationListFragment conversationListFragment = new EaseConversationListFragment();
        conversationListFragment.setConversationListItemClickListener(new EaseConversationListFragment.EaseConversationListItemClickListener() {

            @Override
            public void onListItemClicked(EMConversation conversation) {
                Intent intent = new Intent(MainActivity.this, ChatActivity.class);
                intent.putExtra(EaseConstant.EXTRA_USER_ID, conversation.conversationId());
                if (conversation.isGroup()) {
                    //群聊
                    intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);
                }

                startActivity(intent);
            }
        });
        //联系人fragment
        mContactListFragment = new EaseContactListFragment();
        //设置联系人
        //contactListFragment.setContactsMap(getContacts());
        mContactListFragment.setContactListItemClickListener(new EaseContactListFragment.EaseContactListItemClickListener() {

            @Override
            public void onListItemClicked(EaseUser user) {
                //EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE
                Intent intent = new Intent(MainActivity.this,
                        ChatActivity.class);
                intent.putExtra(EaseConstant.EXTRA_USER_ID,
                        user.getUsername());

                if (user.getType() == 1) {
                    //群聊
                    intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);
                }
                startActivity(intent);
            }
        });
        mFragments.add(conversationListFragment);
        mFragments.add(mContactListFragment);

        mFragments.add(DiscoveryFragment.getInstance());
    }


    @Override
    public void setContacts(Map<String, EaseUser> map) {
        //设置联系人
        mContactListFragment.setContactsMap(map);
    }
}
